/**
 * 自定义公共函数
 */
var myfn = {};
/**
 * 测试函数test1
 * vk.myfn.test1();
 */
myfn.test1 = function(obj = {}) {
	let vk = uni.vk;
	console.log("执行了自定义公共函数test1");
	return obj;
};
let timer = '';
// 定时计算会员是否到期
myfn.updataTime = async function() {
	vk.vuex.set('timeStr', calculateTimeDifference(vk.vuex.get(
		'$user.userInfo.plus_expire_time')))
	clearInterval(timer);
	if (uni.vk.vuex.get('$user.userInfo.plus_type') == 1) {
		timer = setInterval(() => {
			vk.vuex.set('timeStr', calculateTimeDifference(vk.vuex.get(
				'$user.userInfo.plus_expire_time')))
		}, 1000);
	}


};
// 格式化时间
function calculateTimeDifference(targetTimeMs) {
	const currentTime = new Date();
	const targetTime = new Date(targetTimeMs);

	const timeDifference = targetTime - currentTime;
	// console.log("7777777777777", timeDifference);
	const hours = Math.floor(timeDifference / (1000 * 60 * 60));
	const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
	const seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);

	const positiveHours = Math.max(hours, 0);
	const positiveMinutes = Math.max(minutes, 0);
	const positiveSeconds = Math.max(seconds, 0);

	if (timeDifference < 0) {
		clearInterval(timer);
		setTimeout(() => {
			vk.callFunction({
				url: 'client/pages/kh/index_userdata',
				data: {
					todatStart: vk.vuex.get('$app.time.todayStart'),
				},
				success: (res) => {
					console.log("看看到期返回", res);
					login().then(() => {
						console.log("ok登陆了");
					});
				}
			});

		}, 1000)
	}
	if (hours === 0) {
		return `${positiveMinutes}分钟${positiveSeconds}秒`;
	} else {
		if (!positiveHours && !positiveMinutes) return "登陆中"
		return `${positiveHours}小时${positiveMinutes}分钟`;
	}

};

async function login() {
	//检查登录情况，没注册自动用openid注册，注册过的直接登录
	vk.userCenter.loginByWeixin({
		loading: false,
		data: {
			appid: vk.vuex.get('$app.appid'),
			appname: vk.vuex.get('$app.appname'),
			appsecret: "",
			middleman_id: uni.getStorageSync('middleman_id') || 0, //机构/中间人ID
			invite_code: uni.getStorageSync('invite_code') || 0,
			tfid: uni.getStorageSync('tfid') || 0, //投放链接的_id
			req_id: uni.getStorageSync('req_id') || 0, //巨量传进来的req_id
			advertiser_id: uni.getStorageSync('advertiser_id') || 0, //巨量传进来的advertiser_id
			promotion_id: uni.getStorageSync('promotion_id') || 0, //巨量传进来的token
			type: "",
			clue_token: uni.getStorageSync('clue_token') || 0, //巨量传进来的clue_token
			clickid: uni.getStorageSync('clickid') || "",
			tg_pt: uni.getStorageSync('tg_pt') || "",
		},
		success: (data) => {

			this.kefuurl = vk.vuex.get('$app.appinfo.workwxurl');
		}
	});
}

function loadUserData() {
	vk.callFunction({
		url: 'client/pages/kh/index_userdata',
		data: {
			todatStart: vk.vuex.get('$app.time.todayStart'),
		},
		success: (res) => {
			check_uservip(res.userInfo)
		}
	});
};
async function check_uservip(userInfo) {
	if (userInfo.plus_type == 0) {
		console.log("用户非会员");
		return;
	}
	//判断用户的会员到期没有，到期的话就把plus_type改为0
	let plus_type = userInfo.plus_type
	let plus_expire_time = userInfo.plus_expire_time
	let nowtime = new Date().getTime()
	if (plus_expire_time < nowtime && plus_type == 1) {
		console.log("用户会员已过期，改为普通用户");
		let updateVip = await db.collection('uni-id-users').doc(userInfo._id).update({
			plus_type: 0
		})
	}
};
export default myfn;